/**
 * 
 */
package neptune.web.server.security;

import it.robertobifulco.ias.IAS;
import it.robertobifulco.ias.MaximumNumberofOccurenceExceededException;
import it.robertobifulco.ias.Operation;
import it.robertobifulco.ias.OperationList;
import it.robertobifulco.ias.OperationRepository;
import it.robertobifulco.ias.principalmanager.PrincipalRepository;
import it.robertobifulco.ias.rolemanager.RoleRepository;
import neptune.web.server.operations.*;

/**
 * Classe di utilita' per specificare i parametri di inizializzazione dell'IAS.
 * 
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class IASHandler {

	public static IAS getIAS(PrincipalRepository principalRep,
			RoleRepository roleRep, OperationRepository opRep)
			throws IASCreationException {

		try {

			IAS ias = IAS.createIAS(principalRep, roleRep, false, opRep);

			// TODO definizione delle operazioni gestite dall'IAS
			// Studiare un sistema di definizione automatica!!
			OperationList operations = ias.getOperations();

			operations.addOperations(new Operation[] {
					new TopologyConfiguration(), new AddUser(), new GetUsers(),
					new RemoveUser(), new UsersInfo(), new GetUser(),
					new UpdateUser(), new ExperimentCreation(),
					new DeleteExperiment(), new GetExperiment(),
					new AddUserToExperiment(), new RemoveUserFromExperiment(),
					new WriteTopologyFile(), new StartExperiment(),
					new StopExperiment(), new TerminateExperiment(),
					new RequestExperimentStart(), new GetCluster(),
					new ValidateTopology(), new GetVMsHostedOnPm(),
					new GetAllocatedNodes(), new StartNode(), new PauseNode(),
					new RebootNode(), new RemovePhysicalMachine(),
					new AddPhysicalMachine(), new GetPhysicalMachine(),
					new EditPhysicalMachine(), new OpenRandomSSHRedirectPort(),
					new GetAllTemplate(), new GetXMLTemplate(),
					new GetXMLTopology(), new GetXenStatistic(), new GetTask(),
					new RemoveAllFinishedJobs(),
					new GetApplicationPreferences(),
					new UpdateApplicationPreferences(), new SaveTemplate(),
					new DeleteTemplate(), new AllocateExperimentWithLK(),
					new AllocateExperimentWithLKShared(),
					new SaveNodeAsTemplate() });

			// Aggiunge i permission handler per gestire permessi particolari
			// legati ad alcune operazioni
			ias.addPermissionHandler(new DeleteExperimentPH());
			ias.addPermissionHandler(new GetExperimentPH());
			ias.addPermissionHandler(new UpdateExperimentUserPH());
			ias.addPermissionHandler(new WriteTopologyFilePH());
			ias.addPermissionHandler(new ExperimentOpsPH());
			ias.addPermissionHandler(new ValidateTopologyPH());
			ias.addPermissionHandler(new GetAllocatedNodesPH());
			ias.addPermissionHandler(new NodeOpsPH());

			return ias;
		} catch (MaximumNumberofOccurenceExceededException e) {
			throw new IASCreationException(e);
		}

	}
}
